//
//  MineController.m
//  吃货·上海
//
//  Created by 濮亚军 on 15/11/24.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "MineController.h"
#import "MainController.h"
#import <BmobSDK/Bmob.h>
#import "SettingsController.h"// 设置页面
#import "RegistViewController.h"
#import "MyInfoViewController.h"
#import "UIImageView+WebCache.h"
#import "MineHPController.h"
#import "MineModel.h"
@interface MineController ()<UIAlertViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong)UILabel *userLabel; // 显示用户名字

@property (nonatomic,strong)UIButton *cancelBtn;// 注销btn

@property (nonatomic,strong)MineModel *mineModel;

@property (nonatomic, strong)MainController *mainViewVc;

@property (nonatomic,strong)UIView *setView;
@property (nonatomic,strong)UIButton *myInfoBtn;
@property (nonatomic,strong)UIButton *clearBtn;

@end

@implementation MineController
-(void)viewWillAppear:(BOOL)animated
{
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser)
    {
    MineModel *mine = [[MineModel alloc]initWithUserModel:bUser];
    [self.userView.iconImageView sd_setImageWithURL:[NSURL URLWithString:mine.userImageStr] placeholderImage:[UIImage imageNamed:@"textIcon.jpg"]];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    //self.view.backgroundColor = [UIColor redColor];
    self.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - 128);
    self.userView = [[UserView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.userView.hidden = YES;
    [self.view addSubview:_userView];
    
    self.loginView = [[LoginView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.loginView.loginNameTF.delegate = self;
    self.loginView.loginPWTF.delegate = self;
    self.loginView.hidden = YES;
    [self.view addSubview:_loginView];
    
    self.forgetPWView = [[ForgetPWView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.forgetPWView.backgroundColor = KMyColor;
    self.forgetPWView.hidden = YES;
    [self.view addSubview:_forgetPWView];
    
    //判断是否登录
    [self isLogin];
    
    UITapGestureRecognizer *loginTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(loginTapView:)];
    [self.userView.loginImageView addGestureRecognizer:loginTap];
    
    // 注册页面跳转
    UITapGestureRecognizer *registTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(registTapView:)];
    [self.userView.registerImageView addGestureRecognizer:registTap];
    
    
    // 登陆
    UITapGestureRecognizer *login = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(login:)];
    [self.loginView.loginImageView addGestureRecognizer:login];
    
    //返回
    UITapGestureRecognizer *cancleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancleTap:)];
    [self.loginView.cancleImageView addGestureRecognizer:cancleTap];
    
    //忘记密码
    UITapGestureRecognizer *forgetPWTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(forgetPWTap:)];
    [self.loginView.forgetPWImageView addGestureRecognizer:forgetPWTap];
    
    //mine主页
    UITapGestureRecognizer *mineHPTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mineHPTapAction:)];
    [self.userView.mineHPView addGestureRecognizer:mineHPTap];
    
    // 设置界面
    UITapGestureRecognizer *setTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(setTap:)];
    [self.userView.mineSettingsView addGestureRecognizer:setTap];
    
    
}
//判断是否登录
-(void)isLogin
{
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        self.mineModel = [[MineModel alloc]initWithUserModel:bUser];
        self.userView.hidden = NO;
        self.loginView.hidden = YES;
        self.userView.loginImageView.hidden = YES;
        self.userView.registerImageView.hidden = YES;
        
        // 显示用户名字
        self.userLabel = [[UILabel  alloc]initWithFrame:CGRectMake(0, 100 + 50 + 50 + 20 - 20, KScreenWidth, 40)];
        self.userLabel.text = self.mineModel.otherName;
        self.userLabel.textColor = [UIColor redColor];
        self.userLabel.textAlignment = NSTextAlignmentCenter;
        self.userLabel.font = [UIFont boldSystemFontOfSize:25];
        [self.userView addSubview:self.userLabel];
        
        // 显示注销按钮
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cancelBtn.frame = CGRectMake(KScreenWidth - 60, 10, 50, 30);
        self.cancelBtn.backgroundColor = [UIColor greenColor];
        [self.cancelBtn setTitle:@"注销" forState:UIControlStateNormal];
        self.cancelBtn.layer.cornerRadius = 10;
        [self.cancelBtn.layer setMasksToBounds:YES];
        [self.cancelBtn addTarget:self action:@selector(cancelBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_cancelBtn];

    }
    else
    {
        self.userView.hidden = NO;
        self.loginView.hidden = YES;
        self.userView.loginImageView.hidden = NO;
        self.userView.registerImageView.hidden = NO;
    }
}
//前往个人主页
-(void)mineHPTapAction:(UITapGestureRecognizer *)mineHPTap
{
    if (![BmobUser getCurrentUser]) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请先登录" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    MineHPController *mineHPVc = [[MineHPController alloc]init];
    [self.navigationController pushViewController:mineHPVc animated:NO];
}

// 前往设置页面
- (void)setTap:(UITapGestureRecognizer *)tap
{
    if (![BmobUser getCurrentUser]) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请先登录" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    MyInfoViewController *myInfoVc = [[MyInfoViewController alloc]init];
    myInfoVc.mineModel = self.mineModel;
    [self.navigationController pushViewController:myInfoVc animated:NO];
//    self.setView = [[UIView alloc]initWithFrame:CGRectMake(KScreenWidth/2+25, KScreenHeight/2+ 10, KScreenWidth, (KScreenWidth/2 - 60)/2)];
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
//    animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(0, 0)];
//    animation.toValue = [NSValue valueWithCGSize:CGSizeMake(KScreenWidth/2, (KScreenWidth/2 - 60)/2)];
//    animation.duration = 2;
//    [self.setView.layer addAnimation:animation forKey:nil];
//    
//    [self.view addSubview:self.setView];
//    self.myInfoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.myInfoBtn.frame = CGRectMake(0, 0, (KScreenWidth/2 - 60)/2, (KScreenWidth/2 - 60)/2);
//    self.myInfoBtn.backgroundColor = [UIColor greenColor];
//    [self.myInfoBtn setTitle:@"个人中心" forState:UIControlStateNormal];
//    self.myInfoBtn.layer.cornerRadius = (KScreenWidth/2 - 60)/4;
//    [self.myInfoBtn addTarget:self action:@selector(myInfoBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [self.myInfoBtn.layer setMasksToBounds:YES];
//    [self.setView addSubview:self.myInfoBtn];
//    
//    // 清除缓存
//    self.clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.clearBtn.frame = CGRectMake((KScreenWidth/2 - 60)/2 + 20, 0,(KScreenWidth/2 - 60)/2, (KScreenWidth/2 - 60)/2);
//    self.clearBtn.backgroundColor = [UIColor greenColor];
//    [self.clearBtn addTarget:self action:@selector(clearBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [self.clearBtn setTitle:@"清除缓存" forState:UIControlStateNormal];
//    self.clearBtn.layer.cornerRadius = (KScreenWidth/2 - 60)/4;
//    [self.clearBtn.layer setMasksToBounds:YES];
//    [self.setView addSubview:self.clearBtn];
    

}


// 清除缓存
//- (void)clearBtn:(UIButton *)btn
//{
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
//    label.center = self.view.center;
//    label.text = @"缓存已清除";
//    label.font = [UIFont boldSystemFontOfSize:18];
//    label.textAlignment = NSTextAlignmentCenter;
//    label.textColor = [UIColor greenColor];
//    label.alpha = 0;
//    [self.view addSubview:label];
//    
//    [UIView animateWithDuration:1 animations:^{
//        label.alpha = 1;
//    } completion:^(BOOL finished) {
//        if (finished) {
//            [UIView animateWithDuration:1 animations:^{
//                label.alpha = 0;
//            }];
//        }
//    }];
//    
//}


// 个人中心
- (void)myInfoBtn:(UIButton *)button
{
    MyInfoViewController *myInfoVc = [[MyInfoViewController alloc]init];
    myInfoVc.mineModel = self.mineModel;
    [self.navigationController pushViewController:myInfoVc animated:NO];
}


// 通过服务器来验证用户
- (void)login:(UITapGestureRecognizer *)tap
{
    NSLog(@"111");
    [BmobUser loginWithUsernameInBackground:self.loginView.loginNameTF.text password:self.loginView.loginPWTF.text block:^(BmobUser *user, NSError *error) {
        if (user) {
            NSLog(@"登陆成功");
            self.loginView.loginNameTF.text = [[BmobUser getCurrentUser]objectForKey:@"otherName"];
            // 登陆成功
            [self loginsuccess];
            
        }
        else
        {
            NSLog(@"用户名不存在");
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"账号或密码错误,请重新输入" delegate:nil
                                                     cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }
    }];
}
#pragma mark  登陆成功
- (void)loginsuccess
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"成功登陆" delegate:nil
                                             cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    self.userView.hidden = NO;
    self.loginView.hidden = YES;
    self.userView.loginImageView.hidden = YES;
    self.userView.registerImageView.hidden = YES;
    
    // 显示用户名字
    self.userLabel = [[UILabel  alloc]initWithFrame:CGRectMake(0, 100 + 50 + 50 + 20 - 20, KScreenWidth, 40)];
    self.userLabel.text = self.loginView.loginNameTF.text;
    self.userLabel.textColor = [UIColor redColor];
    self.userLabel.textAlignment = NSTextAlignmentCenter;
    self.userLabel.font = [UIFont boldSystemFontOfSize:25];
    [self.userView addSubview:self.userLabel];
    
    // 显示注销按钮
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.frame = CGRectMake(KScreenWidth - 60, 0, 50, 30);
    self.cancelBtn.backgroundColor = [UIColor greenColor];
    [self.cancelBtn setTitle:@"注销" forState:UIControlStateNormal];
    self.cancelBtn.layer.cornerRadius = 10;
    [self.cancelBtn.layer setMasksToBounds:YES];
    [self.cancelBtn addTarget:self action:@selector(cancelBtn:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithCustomView:self.cancelBtn];
//    self.parentViewController.navigationItem.rightBarButtonItem = barBtn;
//    NSLog(@"%@",self.parentViewController);
    [self.view addSubview:_cancelBtn];
    
    // 给用户的model赋值
    [self setBmobUserModel];
    
    [self.userView.iconImageView sd_setImageWithURL:[NSURL URLWithString:self.mineModel.userImageStr] placeholderImage:[UIImage imageNamed:@"textIcon.jpg"]];
}


// 给用户的model赋值
- (void)setBmobUserModel
{
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        self.mineModel = [[MineModel alloc]initWithUserModel:bUser];
    }
    else
    {
        NSLog(@"用户不存在");
    }
}

// 点击注销
- (void)cancelBtn:(UIBarButtonItem *)barBtn
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定注销?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [BmobUser logout];
        self.userView.hidden = YES;
        self.loginView.hidden = NO;
        self.userView.loginImageView.hidden = NO;
        self.userView.registerImageView.hidden = NO;
        self.userLabel.hidden = YES;
        self.cancelBtn.hidden = YES;
        
    }
}
// 注册页面跳转
- (void)registTapView:(UITapGestureRecognizer *)tap
{
    RegistViewController *registVc = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:registVc animated:NO];
}


//登录页面跳转
-(void)loginTapView:(UITapGestureRecognizer *)loginTap
{
    self.userView.hidden = YES;
    self.loginView.hidden = NO;
}
//返回
-(void)cancleTap:(UITapGestureRecognizer *)cancleTap
{
    self.userView.hidden = NO;
    self.loginView.hidden = YES;
}

-(void)forgetPWTap:(UITapGestureRecognizer *)forgetPWTap
{
    self.forgetPWView.hidden = NO;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_loginView.loginNameTF resignFirstResponder];
    [_loginView.loginPWTF resignFirstResponder];
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
//    
//    animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(KScreenWidth/2, (KScreenWidth/2 - 60)/2)];
//    animation.toValue = [NSValue valueWithCGSize:CGSizeMake(0, 0)];
//    animation.duration = 2;
//    [self.setView.layer addAnimation:animation forKey:nil];
    self.setView.hidden = YES;
}

@end
